package cn.kgc.ffzw.day18.homework.chihuolianmeng;

import java.util.Scanner;

/**
 * @author dyy
 * @version 1.0
 * @date 2019/11/24
 * @Description  订单信息类
 * @since JDK1.8
 */
public class Order implements OrederList{
    Scanner d=new Scanner(System.in);
    /**
     *  订单信息静态属性
     */
    Menu menu;
    private int choice;
    private String[] orderlist0={"订单序号","姓名","菜品名称","份数","金额","送达时间","地址","订单状态"};
    private String[][] orderinfo=new String[2][orderlist0.length];
    private int num;
    private int odn=0;
    private int time;

    public Order(Menu menu) {
        this.menu=menu;
    }

    /**
     * 消费者&商家订单查看入口
     */
    @Override
    public void check() {
        if (getOdn()==0){
            System.out.println("暂无订单！");
        }else {
            getOrderinfo()[0] = getOrderlist0();
            for (int i = 0; i <= getOdn(); i++) {
                for (int j = 0; j < getOrderinfo()[0].length; j++) {
                    System.out.print(getOrderinfo()[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println();
        }
    }

    /**
     * 消费者点餐（新增订单）
     */
    @Override
    public void order() {
        menu.showmenu();
        getOrderinfo()[0]=getOrderlist0();
        setOdn(getOdn()+1);
        if (getOdn()>=getOrderinfo().length){
            String[][] newOder=new String[getOdn()+1][getOrderlist0().length];
            System.arraycopy(getOrderinfo(),0,newOder,0,getOrderinfo().length);
            setOrderinfo(newOder);
        }
        System.out.println("请输入要点的菜品序号：" );
        setChoice(d.nextInt());
        getOrderinfo()[getOdn()][0]=String.valueOf(getOdn());
        getOrderinfo()[getOdn()][2]=menu.getMenu()[getChoice()-1];
        System.out.println("请输入要点的数量：");
        setNum(d.nextInt());
        getOrderinfo()[getOdn()][3]=String.valueOf(getNum());
        getOrderinfo()[getOdn()][4]=String.valueOf(getNum()
                *menu.getMenuprice()[getChoice()-1]);
        System.out.println("请输入收货人姓名：");
        getOrderinfo()[getOdn()][1]=d.next();
        System.out.println("请输入送达时间：");
        setTime(d.nextInt());
        getOrderinfo()[getOdn()][5]=String.valueOf(getTime());
        System.out.println("请输入配送地址：");
        getOrderinfo()[getOdn()][6]=d.next();
        getOrderinfo()[getOdn()][7]="已确认";
        System.out.println("订单提交成功！");
    }

    /**
     * 删除订单
     */
    @Override
    public void deletorder() {
        this.check();
        System.out.println("请输入要删除的订单号：");
        setChoice(d.nextInt());
        if (getOrderinfo()[getChoice()][7]=="已签收"){
            System.out.println("不可更改已签收订单信息！");
        }else {
            for (int i = 0; i < getOrderinfo().length - 1; i++) {
                for (int j = 0; j < getOrderinfo()[i].length; j++) {
                    if (i == getChoice()) {
                        getOrderinfo()[i] = getOrderinfo()[i + 1];
                    }
                }
            }
            String[][] newOder = new String[getOrderinfo().length - 1][getOrderinfo()[0].length];
            System.arraycopy(getOrderinfo(), 0, newOder, 0, getOrderinfo().length - 1);
            setOrderinfo(newOder);
            setOdn(getOdn() - 1);
            System.out.println("删除成功！");
            System.out.println();
        }
    }

    /**
     * 签收订单
     */
    @Override
    public void signorder() {
        this.check();
        System.out.println("请输入要签收的订单序号:");
        setChoice(d.nextInt());
        if (getOrderinfo()[getChoice()][7]=="已签收"){
            System.out.println("此订单已被签收过！");
        }else {
            getOrderinfo()[getChoice()][7]="已签收";
        }
        System.out.println("签收成功！");

    }

    public int getChoice() {
        return choice;
    }

    public void setChoice(int choice) {
        this.choice = choice;
    }

    public String[] getOrderlist0() {
        return orderlist0;
    }

    public void setOrderlist0(String[] orderlist0) {
        this.orderlist0 = orderlist0;
    }

    public String[][] getOrderinfo() {
        return orderinfo;
    }

    public void setOrderinfo(String[][] orderinfo) {
        this.orderinfo = orderinfo;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getOdn() {
        return odn;
    }

    public void setOdn(int odn) {
        this.odn = odn;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
        while (this.time<8||this.time>22){
            System.out.println("不在配送时间段，请重新输入：");
            this.time=d.nextInt();
        }
    }
}
